package org.openkoala.koala.wizard.wizardpage;

import java.util.List;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.Project;

public class InfoSummaryWizardPage extends WizardPage {

	private Project project;

	//项目信息组件
	private Label projectName;
	private Label groupId;
	private Label artifactId;
	private Label version;
	private Label descirption;
	private Text modulesText;
	
	public InfoSummaryWizardPage(Project project) {
		super("Project Information");
		this.project = project;
		setTitle("Project Information");
		setDescription("View all project informations.");
		setPageComplete(false);
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		
		initializeDialogUnits(composite);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);

		generateProjectBaseInfoGroup(composite);
		generateMavenInfoGroup(composite);
		generateModulesGroup(composite);
		setProjectInfoValue();

		setControl(composite);
		setPageComplete(true);
	}

	/**
	 * 生成项目基本信息块
	 * 
	 * @param composite
	 */
	private void generateProjectBaseInfoGroup(Composite composite) {
		GridData projectGridData = new GridData(GridData.FILL_HORIZONTAL);
		projectGridData.grabExcessHorizontalSpace = true;

		Group projectGroup = new Group(composite, SWT.NULL);
		projectGroup.setText("Project");
		projectGroup.setLayout(new GridLayout(2, false));
		projectGroup.setLayoutData(projectGridData);

		Label projectNameLabel = new Label(projectGroup, SWT.NONE);
		projectNameLabel.setText("Project Name: ");
		projectName = new Label(projectGroup, SWT.NONE);
		projectName.setLayoutData(projectGridData);
	}
	
	/**
	 * 生成maven信息块
	 * 
	 * @param composite
	 */
	private void generateMavenInfoGroup(Composite composite) {
		GridData mavenInfoGridData = new GridData(GridData.FILL_HORIZONTAL);
		mavenInfoGridData.grabExcessHorizontalSpace = true;

		Group mavenInfoGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
		mavenInfoGroup.setText("Maven");
		mavenInfoGroup.setLayout(new GridLayout(2, false));
		mavenInfoGroup.setLayoutData(mavenInfoGridData);

		Label groupIdLabel = new Label(mavenInfoGroup, SWT.NONE);
		groupIdLabel.setText("groupId: ");
		groupId = new Label(mavenInfoGroup, SWT.NONE);
		groupId.setLayoutData(mavenInfoGridData);

		Label artifactIdLabel = new Label(mavenInfoGroup, SWT.NONE);
		artifactIdLabel.setText("artifactId: ");
		artifactId = new Label(mavenInfoGroup, SWT.NONE);
		artifactId.setLayoutData(mavenInfoGridData);

		Label versionLabel = new Label(mavenInfoGroup, SWT.NONE);
		versionLabel.setText("version: ");
		version = new Label(mavenInfoGroup, SWT.NONE);
		version.setLayoutData(mavenInfoGridData);
		
		Label descirptionLabel = new Label(mavenInfoGroup, SWT.NONE);
		descirptionLabel.setText("descirption: ");
		descirption = new Label(mavenInfoGroup, SWT.NONE);
		descirption.setLayoutData(mavenInfoGridData);
	}

	/**
	 * 生成子模块信息块
	 * 
	 * @param composite
	 */
	private void generateModulesGroup(Composite composite) {
		// 子模块信息
		GridData modulesGroupGridData = new GridData(GridData.FILL_HORIZONTAL);
		modulesGroupGridData.grabExcessHorizontalSpace = true;

		Group modulesGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
		modulesGroup.setText("Modules");
		modulesGroup.setLayout(new GridLayout(1, false));
		modulesGroup.setLayoutData(modulesGroupGridData);

		GridData moduleGroupGridData = new GridData(GridData.FILL_HORIZONTAL);
		moduleGroupGridData.heightHint = 200;
		modulesText = new Text(modulesGroup, SWT.V_SCROLL);
		modulesText.setEditable(false);
		modulesText.setLayoutData(moduleGroupGridData);
	}

	/**
	 * 设置各项项目信息的值
	 */
	private void setProjectInfoValue() {
		projectName.setText(project.getAppName());
		groupId.setText(project.getGroupId());
		artifactId.setText(project.getArtifactId());
		version.setText(project.getVersion());
		
//		if (project.getDescription() == null || project.getDescription().isEmpty()) {
//			project.setDescription(" ");
//		}
		descirption.setText(project.getDescription());
		
		modulesText.setText(getModulesInfo());
	}
	
	private String getModulesInfo() {
		StringBuilder modulesInfo = new StringBuilder();
		for (Module module : project.getModule()) {
			modulesInfo.append("Module Type:             ").append(module.getModuleType()).append("\n");	//TODO 空格为了使冒号后面内容对齐，需要更好的解决方案。
			modulesInfo.append("Module Name:           ").append(module.getModuleName()).append("\n");		//TODO 空格为了使冒号后面内容对齐，需要更好的解决方案。

			String technologyInfo = generateDependencyInfo(module.getFunctions());
			if (!technologyInfo.isEmpty()) {
				modulesInfo.append("Function Dependency: ").append(technologyInfo).append("\n");	//TODO 空格为了使冒号后面内容对齐，需要更好的解决方案。
			}
			
			String moduleDependencyInfo = generateDependencyInfo(module.getDependencies());
			if (!moduleDependencyInfo.isEmpty()) {
				modulesInfo.append("Module Dependency:  ").append(moduleDependencyInfo).append("\n");	//TODO 空格为了使冒号后面内容对齐，需要更好的解决方案。
			}
			
			modulesInfo.append("\n");
		}

		return modulesInfo.toString();
	}
	
	private String generateDependencyInfo(List<String> dependencies) {
		StringBuilder dependenciesInfo = new StringBuilder();
		for (String technology : dependencies) {
			dependenciesInfo.append(technology).append(",");
		}

		if (dependenciesInfo.toString().isEmpty()) {
			return dependenciesInfo.toString();
		}
		return dependenciesInfo.substring(0, dependenciesInfo.length() - 1);
	}
	
	/**
	 * 刷新项目信息汇总展示
	 */
	public void refreshProjectInfo() {
		setProjectInfoValue();
	}

}
